import { useRequest } from '@umijs/hooks';
import { message } from 'antd'
import { queryProductList, removeProducts } from '@/utils/axios/api';

export default function useProductList() {
  const { tableProps, params, refresh } = useRequest(({ current, pageSize, sorter: s, filters: f }) => {
    const p: any = { current, pageSize };
    if (s?.field && s?.order) {
      p[s.field] = s.order;
    }
    if (f) {
      Object.entries(f).forEach(([filed, value]) => {
        p[filed] = value;
      });
    }
    return queryProductList(p);
  }, {
    paginated: true,
    defaultPageSize: 10
  });
  console.log(tableProps);
  
  const deleteProducts = async (id: string) => {
    try {
      await removeProducts(id);
      message.success('success');
      refresh()
    } catch (error) {
      message.error('fail');
    }
  };
  return {
    tableProps, params, refresh, deleteProducts
  };
}

